package com.xjwn.datasync.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

//工具类
//用来从spring容器里获取bean
@Component
public class SpringContextUtils implements ApplicationContextAware {

    //静态变量
    private static ApplicationContext applicationContext;
  
    @Override
    //项目初始化注入ApplicationContext对象
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringContextUtils.applicationContext = applicationContext;  
    }

    //通过bean名字获取bean
    public static Object getBean(String name) {  
        return applicationContext.getBean(name);  
    }  

    //通过类名称获取bean
    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }  

    //通过bean名字和类名称获取bean
    public static <T> T getBean(String name, Class<T> requiredType) {
        return applicationContext.getBean(name, requiredType);  
    }  

    //检查容器中是否包含指定名称的bean
    public static boolean containsBean(String name) {  
        return applicationContext.containsBean(name);  
    }  

    //检查指定bean是否是单例模式
    public static boolean isSingleton(String name) {  
        return applicationContext.isSingleton(name);  
    }  

    //根据bean的名称获取该bean的类型
    public static Class<? extends Object> getType(String name) {
        return applicationContext.getType(name);  
    }  
}